/*
 * @Description: 
 * @Author: Jimmy
 * @Date: 2023-07-23 22:51:41
 * @LastEditTime: 2023-07-23 23:21:14
 * @LastEditors:  
 */
import bcrypt from 'bcryptjs'
import { Context, Next } from "koa";
import validate from "../utils/validator";
import { Rules } from "async-validator";
import UseService from '../service/user.service'
import { callbackData } from "../utils/data.handler";
import { access, sql } from "../logger";

//创建用户信息基本规则校验
export const baseUserValidator = async (ctx:Context,next:Next)=>{
    const rules: Rules = {
        username: {
            type: 'string',
            required: true,
            message: '用户名不能为空'
        },
        nickname: {
            type: 'string',
            required: true,
            message: '昵称不能为空'
        },
        phone: {
            type: 'string',
            required: true,
            message: '手机不能为空'
        },
        password: {
            type: 'string',
            required: true,
            message: '密码不能为空'
        }
    }
    const { error } = await validate<any>(ctx, rules);
    if(error){
        access.info('用户信息校验错误',{msg:error})
        console.error('用户信息校验错误',{msg:error})
        ctx.app.emit('error',callbackData('1',error),ctx);
        return false;
    }
    await next();
}
